import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { StateType } from './typings'
import {
  getPatrolConfigList,
  getPatrolGroupList,
  getPatrolGroupRemoveList,
  getPatrolModule,
  getPatrolHandleUser,
  getPatrolConfigUpdateList,
  getPatrolTaskList,
  getPatrolTaskRemoveList,
  getDictionary,
  getPatrolRecordList,
  getSllAssetPage,
  getConfigList,
  generateCycle,
  getRoadList,
  patrolRecordView,
  patrolGroupSave,
  patrolGroupAdd,
  patrolGroupView,
  patrolTaskView,
  getPatrolConfigAddList,
  workareaSelectList
} from '~/api/oam/tour'
import { filterValueEnum } from '~/utils/util'
import { getDataDictionaryByCode } from '~/api/global'

const tour: Module<StateType, StateType> = {
  namespaced: true,
  state: {
    personInChargeOpt: [],
    designateOpt: [
      { id: '1', name: '已指派' },
      { id: '2', name: '未指派' }
    ],
    accomplishOpt: [],
    periodOpt: [],
    patrolBodyOpt: [],
    patrolStateOpt: [],
    isPlanOpt: [
      { id: '0', name: '否' },
      { id: '1', name: '是' }
    ],
    patrolTypeOpt: [],
    ormOrderStatusEnum: {},
    //  所属工区
    deptTreeSelectOpt: []
  },
  getters: {
    personInChargeOpt: (state) => state.personInChargeOpt,
    designateOpt: (state) => state.designateOpt,
    accomplishOpt: (state) => state.accomplishOpt,
    periodOpt: (state) => state.periodOpt,
    patrolBodyOpt: (state) => state.patrolBodyOpt,
    patrolStateOpt: (state) => state.patrolStateOpt,
    isPlanOpt: (state) => state.isPlanOpt,
    patrolTypeOpt: (state) => state.patrolTypeOpt,
    ormOrderStatusEnum: (state) => state.ormOrderStatusEnum,
    deptTreeSelectOpt: (state) => state.deptTreeSelectOpt
  },
  mutations: {
    SET_PATROL_MODULE(state, payload) {
      state.patrolBodyOpt = payload
    },
    SET_PATROL_HANDLE_USER(state, payload) {
      state.personInChargeOpt = payload
    },
    GET_ACCOMPLISH(state, payload) {
      state.accomplishOpt = payload
    },
    GET_PERIOD(state, payload) {
      state.periodOpt = payload
    },
    GET_PATROl_STATE(state, payload) {
      state.patrolStateOpt = payload
    },
    GET_CONFIGLIST(state, payload) {
      state.patrolTypeOpt = payload
    },
    GET_ORMORDERSTATUS(state, payload) {
      state.ormOrderStatusEnum = filterValueEnum(payload, 'dictKey', 'dictValue')
    },
    SET_WORKAREASELECT(state, payload) {
      state.deptTreeSelectOpt = payload
    }
  },
  actions: {
    // 巡视设置列表
    GetPatrolConfigList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getPatrolConfigList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 巡视设置列表-编辑
    GetPatrolConfigUpdateList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getPatrolConfigUpdateList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 巡视设置列表-新增
    GetPatrolConfigAddList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getPatrolConfigAddList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 查询巡视主体
    async GetPatrolModule<S, T>({ commit }: ActionContext<S, T>) {
      return await getPatrolModule({}).then((res: AxiosResponse) => {
        commit('SET_PATROL_MODULE', res?.data?.data || [])
        return res?.data
      })
    },
    // 巡视计划列表
    GetPatrolGroupList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getPatrolGroupList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 巡视计划列表-删除
    GetPatrolGroupRemoveList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getPatrolGroupRemoveList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 巡视计划列表-详情
    PatrolGroupView<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        patrolGroupView<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 巡视任务列表
    GetPatrolTaskList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getPatrolTaskList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 巡视任务列表-详情
    PatrolTaskView<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        patrolTaskView<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 巡视任务列表-删除
    GetPatrolTaskRemoveList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getPatrolTaskRemoveList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 巡视记录列表
    GetPatrolRecordList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getPatrolRecordList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 台区列表
    GetSllAssetPage<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getSllAssetPage<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 巡视计划列表-修改
    PatrolGroupSave<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        patrolGroupSave<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 巡视计划列表-新增
    patrolGroupAdd<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        patrolGroupAdd<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 根据配置生成最近5次任务生成周期
    GenerateCycle<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        generateCycle<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 巡视计划 - 道路巡视列表
    GetRoadList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getRoadList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 巡视记录列表-详情
    PatrolRecordView<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        patrolRecordView<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 巡视记录-所属工区
    async WorkareaSelectList<S, T>({ commit }: ActionContext<S, T>) {
      return await workareaSelectList({}).then((res: AxiosResponse) => {
        commit('SET_WORKAREASELECT', res?.data?.data || [])
        return res?.data
      })
    },
    // 字典-查询负责人
    async GetPatrolHandleUser<S, T>({ commit }: ActionContext<S, T>) {
      return await getPatrolHandleUser({}).then((res: AxiosResponse) => {
        commit('SET_PATROL_HANDLE_USER', res?.data?.data || [])
        return res?.data
      })
    },
    // 字典-工单状态
    async GetOrmOrderStatus<S, T>({ commit }: ActionContext<S, T>) {
      return await getDataDictionaryByCode({ code: 'orm_order_status' }).then(
        (res: AxiosResponse) => {
          commit('GET_ORMORDERSTATUS', res?.data?.data || [])
          return res?.data
        }
      )
    },
    // 字典-完成情况
    async GetDictionaryAccomplish<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return await getDictionary(params).then((res: AxiosResponse) => {
        commit('GET_ACCOMPLISH', res?.data?.data || [])
        return res?.data
      })
    },
    // 字典-巡视类型
    async GetConfigList<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return await getConfigList(params).then((res: AxiosResponse) => {
        commit('GET_CONFIGLIST', res?.data?.data || [])
        return res?.data
      })
    },
    // 字典-查看周期
    async GetDictionaryPeriod<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return await getDictionary(params).then((res: AxiosResponse) => {
        commit('GET_PERIOD', res?.data?.data || [])
        return res?.data
      })
    },
    // 字典-巡视状态
    async GetPatrolState<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return await getDictionary(params).then((res: AxiosResponse) => {
        commit('GET_PATROl_STATE', res?.data?.data || [])
        return res?.data
      })
    }
  }
}

export default tour
